#ifndef _MENU_H
#define _MENU_H

#include "../includes.h"

void Menu(void)
{
    // initialise main GDK stuff
    dbBackdropOn();
    dbRandomize(dbTimer());
    loadMenuMedia();

    do
    {
        // if the mouse is over play button, delete menu media and go to game, then reload media once game loop is done.
        if (MouseOver(1) && dbMouseClick() == 1)
        {
            flushMenuMedia();
            MainLoop();
            loadMenuMedia();
            dbFlushVideoMemory();
            ShowCursor(true);
        }

        // if the mouse is over the quit button
        if (MouseOver(2) && dbMouseClick() == 1)
        {
            flushMenuMedia();
            break;
        }
        dbSync();
    }
    while (windowEvent() != WM_CLOSE);

    flushMenuMedia();
    return;
}

void loadMenuMedia(void)
{
    if (!dbIsImage(1))
    {
        dbLoadImage("media/play.png",1);
        dbSprite(1, (dbScreenWidth() / 2) - (dbImageWidth(1)/2), 200, 1);
        dbLoadImage("media/quit.png",2);
        dbSprite(2, (dbScreenWidth() / 2) - (dbImageWidth(2)/2), 300, 2);
    }
    return;
}

void flushMenuMedia(void)
{
    if (dbIsImage(1))
    {
        dbDeleteSprite(1);
        dbDeleteImage(1);
    }
    if (dbIsImage(2))
    {
        dbDeleteSprite(2);
        dbDeleteImage(2);
    }
    return;
}

#endif
